#!/bin/bash
#nohup sh refresh_rank.sh > _ranksh.pid 2>&1 &

now=0
last=0
lastday=0
nowday=0

cmd="{\"msghead\":{\"msgname\":\"renew_rank\"},\"msgbody\":{\"rankname\":\"diamondrank\",\"linenum\":30,\"ranktype\":2}}"

#刷新所有排行榜
refresh_all_rank(){
	if [ $nowday -ne $lastday ]
	then
		lastday=$nowday
		line=$(redis-cli -n 15 rpush requestmq renew_rank 30)
		#echo "refresh all rank"
	fi
}

#刷新金豆排行榜
refresh_diamond_rank(){
	dur=$[$now-$last]
	if [ $dur -ge 300 ]
	then
		last=$now
		line=$(redis-cli -n 15 rpush requestmq $cmd)
		#echo "refresh diamond rank"
	fi
}

#更新当前时间
update_time(){
	now=$(date "+%s")
	nowday=$(date "+%d")
}

#后台运行
echo "PID: $$" > _ranksh.pid

#不断循环刷新
while :
do
	update_time
	refresh_diamond_rank
	refresh_all_rank
	sleep 60 #300s更新一次金豆排行
done
